package test.serverframe.armc.server.manager.service.exec;

import com.microcore.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import test.serverframe.armc.server.manager.dao.mapper.TaskEnterInterfaceMapper;
import test.serverframe.armc.server.manager.domain.TaskEnterInterface;
import test.serverframe.armc.server.manager.service.exec.dag.DagNode;
import test.serverframe.armc.server.manager.service.exec.dag.NodeInfo;
import test.serverframe.armc.server.manager.service.exec.params.InterfaceExecutorParams;
import test.serverframe.armc.server.manager.service.exec.params.TaskExecutorParams;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 接口
 *
 * @author LiuChunfu
 * @date 2019.01.19
 */
public class TaskExecutor extends BaseExecutor {

    private Logger logger = LoggerFactory.getLogger(TaskExecutor.class);

    /**
     * 接口执行参数
     */
    private TaskExecutorParams params;

    private TaskEnterInterfaceMapper taskEnterInterfaceMapper = SpringContextUtils.getBean("taskEnterInterfaceMapper", TaskEnterInterfaceMapper.class);

    /**
     * 缓存
     */
    private Map<String, String> map = new HashMap<>();
    
    private AtomicInteger interfaceTotal = new AtomicInteger(0);
    
    private AtomicInteger successInterfaceTotal = new AtomicInteger(0);
    
    public TaskExecutor(TaskExecutorParams params) {
        this.params = params;
    }

    @Override
    public List<NodeInfo> getNodeInfo() {
        List<TaskEnterInterface> list = taskEnterInterfaceMapper.findInterByTaskId(params.getTaskId());
        ArrayList<NodeInfo> nodeList = new ArrayList<>();
        list.forEach(inter -> {
            NodeInfo nodeInfo = new NodeInfo(inter.getNodeId(), convert2List(inter.getParentIds()), convert2List(inter.getChildIds()));
            nodeList.add(nodeInfo);
        });
        return nodeList;
    }

    @Override
    public Runnable getNodeExecutor(DagNode node) {
        try {
            //接口执行所需要的参数
            InterfaceExecutorParams interfaceExecutorParams = new InterfaceExecutorParams();
            interfaceExecutorParams.setNodeId(node.getId());
            interfaceExecutorParams.setTaskId(params.getTaskId());
            interfaceExecutorParams.setSuccessInterfaceTotal(successInterfaceTotal);
            interfaceExecutorParams.setInterfaceTotal(interfaceTotal);
            interfaceExecutorParams.setExpectedInterfaceTotal(getNodeListSize());
            interfaceExecutorParams.setTaskTotal(params.getTaskTotal());
            interfaceExecutorParams.setSuccessTaskTotal(params.getSuccessTaskTotal());
            interfaceExecutorParams.setExpectedTaskTotal(params.getExpectedTaskTotal());
            interfaceExecutorParams.setInvokeRecordId(params.getInvokeRecordId());
            interfaceExecutorParams.setTaskRecordId(params.getTaskRecordId());
            interfaceExecutorParams.setCache(map);
            interfaceExecutorParams.setInterfaceDagNode(node);
            interfaceExecutorParams.setTaskDagNode(params.getDagNode());
            interfaceExecutorParams.setFailedStrategy(params.getFailedStrategy());
            interfaceExecutorParams.setTaskInvokeId(params.getTaskInvokeId());
            interfaceExecutorParams.setIsReStart(params.getIsReStart());
            logger.info("interfaceExecutorParams: " + interfaceExecutorParams);
            return new InterfaceExecutor(interfaceExecutorParams);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        } 
        return null;
    }
}
